package com.beta.service.impl;

import com.baomidou.framework.service.impl.SuperServiceImpl;
import com.beta.dto.product.ProductPropertiesDto;
import com.beta.entity.ProductProperties;
import com.beta.mapper.ProductPropertiesMapper;
import com.beta.service.IProductPropertiesService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * ProductProperties 表数据服务层接口实现类
 */
@Service
public class ProductPropertiesService extends SuperServiceImpl<ProductPropertiesMapper, ProductProperties> implements IProductPropertiesService {

    @Autowired
    private ProductPropertiesMapper productPropertiesMapper;

    @Override
    public List<ProductPropertiesDto> getProductProperties(Integer productId) {
        Map<Integer, List<ProductPropertiesDto>> batchResult = getProductPropertiesBatch(Arrays.asList(productId));
        if (MapUtils.isEmpty(batchResult) || CollectionUtils.isEmpty(batchResult.get(productId))) {
            return Collections.emptyList();
        } else {
            return batchResult.get(productId);
        }
    }

    @Override
    public Map<Integer, List<ProductPropertiesDto>> getProductPropertiesBatch(List<Integer> productIds) {
        if (CollectionUtils.isEmpty(productIds)) {
            return Collections.emptyMap();
        }
        List<ProductPropertiesDto> productPropertiesDtos = productPropertiesMapper.selectProductPropertiesBatch(productIds);
        return productPropertiesDtos.stream().collect(Collectors.groupingBy(ProductPropertiesDto::getProductId));
    }

}